using Microsoft.AspNetCore.Mvc;
using Beauty.Net.Elasticsearch;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;

namespace Beauty.NetWebApi.Controllers
{
    /// <summary>
    /// Elasticsearch 演示控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class ElasticsearchDemoController : ControllerBase
    {
        private readonly IElasticsearchService _elasticsearchService;
        private readonly ILogger<ElasticsearchDemoController> _logger;

        public ElasticsearchDemoController(IElasticsearchService elasticsearchService, ILogger<ElasticsearchDemoController> logger)
        {
            _elasticsearchService = elasticsearchService;
            _logger = logger;
        }

        /// <summary>
        /// 索引单个文档
        /// </summary>
        [HttpPost("index")]
        public async Task<IActionResult> IndexDocument([FromQuery] string indexName, [FromBody] dynamic document)
        {
            if (string.IsNullOrEmpty(indexName))
            {
                return BadRequest(new { message = "索引名称不能为空" });
            }

            var result = await _elasticsearchService.IndexAsync(indexName, document);
            _logger.LogInformation($"文档索引成功: {indexName}, {result.Id}");
            return Ok(new { message = "文档索引成功", success = result.IsValid, id = result.Id });
        }

        /// <summary>
        /// 获取文档
        /// </summary>
        [HttpGet("document/{id}")]
        public async Task<IActionResult> GetDocument([FromQuery] string indexName, string id)
        {
            if (string.IsNullOrEmpty(indexName))
            {
                return BadRequest(new { message = "索引名称不能为空" });
            }
            if (string.IsNullOrEmpty(id))
            {
                return BadRequest(new { message = "文档ID不能为空" });
            }

            var document = await _elasticsearchService.GetAsync<dynamic>(indexName, id);
            if (document == null)
            {
                return NotFound(new { message = "文档不存在", indexName, id });
            }

            return Ok(new { message = "文档获取成功", document });
        }

        /// <summary>
        /// 更新文档
        /// </summary>
        [HttpPut("document/{id}")]
        public async Task<IActionResult> UpdateDocument([FromQuery] string indexName, string id, [FromBody] dynamic document)
        {
            if (string.IsNullOrEmpty(indexName))
            {
                return BadRequest(new { message = "索引名称不能为空" });
            }
            if (string.IsNullOrEmpty(id))
            {
                return BadRequest(new { message = "文档ID不能为空" });
            }

            var result = await _elasticsearchService.UpdateAsync(indexName, id, document);
            _logger.LogInformation("文档更新成功: {IndexName}, {DocumentId}", indexName, id);
            return Ok(new { message = "文档更新成功", success = result.IsValid });
        }

        /// <summary>
        /// 删除文档
        /// </summary>
        [HttpDelete("document/{id}")]
        public async Task<IActionResult> DeleteDocument([FromQuery] string indexName, string id)
        {
            if (string.IsNullOrEmpty(indexName))
            {
                return BadRequest(new { message = "索引名称不能为空" });
            }
            if (string.IsNullOrEmpty(id))
            {
                return BadRequest(new { message = "文档ID不能为空" });
            }

            var result = await _elasticsearchService.DeleteAsync(indexName, id);
            _logger.LogInformation($"文档删除成功: {indexName}, {id}");
            return Ok(new { message = "文档删除成功", success = result.IsSuccess });
        }
    }
}